<?php
class Frontend_View_Helper_AuthInfo extends Zend_View_Helper_Abstract
{
    /**
     * @var Wenda_Service_Authentication
     */
    protected $_authService;

    /**
     * Get user info from the auth session
     *
     * @param string|null $info
     * @return null|Zend_View_Helper_AuthInfo
     */
    public function authInfo($info = null)
    {
        if (null === $this->_authService) {
            $this->_authService = Wenda::getService('authentication');
        }

        if (null === $info) {
            return $this;
        }

        if (false === $this->isLogined()) {
            return null;
        }

        return $this->_authService->getIdentity()->$info;
    }

    /**
     * Check if we are logged in
     *
     * @return boolean
     */
    public function isLogined()
    {
        return $this->_authService->getAuth()->hasIdentity();
    }

    /**
     * Get login user info
     *
     * @param string|null $option
     * @return string|array
     */
    public function getUser($option = null)
    {
        if (!$this->isLogined()) {
            return null;
        }

        $user = $this->_authService->getAuth()->getStorage()->read();
        if (null === $option) {
            return $user;
        } else {
            return (array_key_exists($option,$user)) ? $user[$option] : false;
        }
    }
}
